package com.shanpin.shebao.admin.module.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shanpin.shebao.admin.module.city.CityCacheService;
import com.shanpin.shebao.admin.module.city.CityService;
import com.shanpin.shebao.admin.module.shebao.SBConfigDao;
import com.shanpin.shebao.admin.module.shebao.model.ShebaoConfigData;
import com.shanpin.shebao.admin.module.user.model.BuyedDetail;
import com.shanpin.shebao.model.City;
import com.shanpin.shebao.model.ServiceResult;
import com.shanpin.shebao.utility.PageModel;

@Service
public class BuyedService {

	@Autowired
	private CityCacheService cityCacheService;
	@Autowired
	private SBConfigDao sbConfigDao;
	@Autowired
	private BuyedDao buyedDao;
	
	public PageModel loadList(int page, int size, String id_card) {
		PageModel pm = new PageModel();
		int start = (page - 1) * size;
		pm.setPageNo(page);
		pm.setPageSize(size);
		buyedDao.loadList(start, pm, id_card);
		List<BuyedDetail> list = pm.getRecordList();
		City city = null;
		for (BuyedDetail detail : list) {
			city = cityCacheService.get(detail.getCity_code());
			detail.setCity_name(city == null ? "----" : city.getCity_name());
		}
		return pm;
	}
	
	public ServiceResult<Boolean> edit(int id, int cid, double fund_price) {
		BuyedDetail bd = buyedDao.get(id);
		if (null == bd) {
			return ServiceResult.valueOf("未找到控制记录");
		}
		boolean ret = false;
		if (bd.getCid() != cid) {
			ShebaoConfigData configData = sbConfigDao.getShebao(cid);
			if (null != configData && configData.getCity_code() == bd.getCity_code()) {
				ret = buyedDao.editCid(id, cid);
			} else {
				return ServiceResult.valueOf("指定的档位不是当前城市的");
			}
		}
		if (bd.getFund_price() != fund_price) {
			ret = buyedDao.editFund(id, fund_price);
		}
		return ServiceResult.valueOf(ret ? "" : "操作出现错误", ret);
	}
	
}
